'use strict'

// $(function() {
    angular.module("NetworkRequest", [])
        .config(function ($httpProvider) {
            // $httpProvider.defaults.withCredentials = true;//允许发送cookie
            // $httpProvider.defaults.headers.common['Authorization'] = document.cookie.split(";")[0].split("user_token=")[1];
        })
        .controller("NetworkRequestCtro", ['$scope', '$http', function ($scope, $http) {
            // 读取cookie eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjEifQ==.838a31f45466dbdcb4b8f0ed14322bb4df392fd0365126af73d4cc13680035b6
            $scope.user_token = document.cookie.split(";")[0].split("user_token=")[1]
            console.log( $scope.user_token)
            if($scope.user_token !== ''){
                console.log(document.cookie)
                $scope.loginStatus = "已登录"
                console.log($scope.loginStatus)
            }else{
                $scope.loginStatus = "未登录"
                console.log($scope.loginStatus)
                window.history.go(-1)
            }
            //退出登录
            $scope.logoutClick = function () {
                document.cookie="user_token="+"";
                $scope.user_token = ""
                console.log( document.cookie)
                window.location.href = $scope.router.tologin
                // $cookieStore.remove(user_token)
            }



            // 页面跳转
            $scope.router = {
                // 首页面
                tologin:'./login.html',
                toArticle: "./public/views/publish.html#/article",
                toVideo: "./public/views/publish.html#/video",
                toTopic: "./public/views/publish.html#/topic",
                returnArticle:"http://120.79.18.188/#/article",
                returnVideo:"http://120.79.18.188/#/video",
                returnTopic:"http://120.79.18.188/#/topic",
                returnLogin:"http://120.79.18.188/login.html",

                // 详情页面
                toArticleDetail: "public/views/detail.html?type=atricle&articleId=",
                toVideoDetail: "public/views/detail.html?type=video&videoId=",
                toTopicDetail: "public/views/detail.html?type=topic&topicId=",

            }

           // 请求url
            $scope.url = {
                base: "http://120.79.18.188",
                // base:"http://testdomain.xiaokuango.com",
                login: "/api/v1/admin/login",//管理员登录
                getArticleList: "/api/v1/admin/article/list",//获取文章列表
                getArticleDetail: "/api/v1/admin/article/view",//获取文章详情
                getVideoList: "/api/v1/admin/video/list",//获取视频列表
                getVideoDetail: "/api/v1/admin/video/view",//获取视频详情
                getTopicList: "/api/v1/admin/topic/list",//获取文章列表
                getTopicDetail: "/api/v1/admin/topic/view",//获取文章详情
                addArticle: "/api/v1/admin/article/add",//添加文章
                addVideo: "/api/v1/admin/video/add",//添加视频
                addTopic: "/api/v1/admin/topic/add",//添加话题
                deleteArticle: "/api/v1/admin/article/delete",//删除文章
                deleteVideo: "/api/v1/admin/video/delete",//删除视频
                deleteTopic: "/api/v1/admin/topic/delete",//删除话题
                reply: "/api/v1/admin/comment/reply",//回复评论
                imageUpload: "/api/v1/admin/upload_image",//图片上传
                ueditorUpload: "/api/v1/admin/uploadimage",//富文本图片上传
                videoUpload: "/api/v1/admin/video/upload",//视频上传

            }



            //注意$http get 拼接参数用params   post用data
            $scope.GET = function (url, paramter, success, error) {



                $http({
                    method: 'GET',
                    url: $scope.url.base + url,
                   // url: url,
                   //  xhrFields: {
                   //      withCredentials: true
                   //   },
                    // contentType:false,
                    // processData:false,
                    cache:true,
                    crossDomain:true,
                    params: paramter,
                    headers: {
                        // 'Access-Control-Allow-Origin':true,
                        'Content-Type': 'application/json',
                        'Authorization': $scope.user_token//"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjEifQ==.838a31f45466dbdcb4b8f0ed14322bb4df392fd0365126af73d4cc13680035b6"
                    },
                }).success(
                    function (res) {
                        success(res)
                    },
                ).error(
                    function (error) {
                        error(error)
                    }
                )
            }

            $scope.POST = function (url, paramter, success, error) {
                console.log(url, paramter)
                $http({
                    method: 'POST',
                    url: $scope.url.base + url,
                    data: paramter,
                    header: {
                        'content-type': 'application/json',
                        Authorization: $scope.user_token
                    },
                }).success(
                    function (res) {
                        success(res)
                    },
                ).error(
                    function (error) {
                        error(error)
                    }
                )
            }
            $scope.ajaxForm = function (url, formData, success, fail) {
                console.log(url,formData)
                $.ajax({
                    url: $scope.url.base + url,
                    data: formData,
                    type: 'POST',
                    crossDomain:true,
                    contentType: false,
                    processData: false,
                    headers: {
                        'Authorization': $scope.user_token//'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjEifQ==.838a31f45466dbdcb4b8f0ed14322bb4df392fd0365126af73d4cc13680035b6'
                    },
                    success: function (res) {
                        success(res)

                    },
                    error: function (err) {
                        fail(err)
                    }
                })

            }
            //获取文章列表
            $scope.getArticleList = function (page, count, success, fail) {
                var parameter = {
                    "page": page,
                    "count": count,
                    // "jsonp":"JSON_CALLBACK"
                }
                // console.log(parameter, success, fail)
                $scope.GET($scope.url.getArticleList, parameter, success, fail)
            }
            //获取文章详情
            $scope.getArticleDetail = function ( articleId, success, fail) {
                var parameter = {
                  "articleId":articleId,
                }
                console.log( parameter, success, fail)
                $scope.GET($scope.url.getArticleDetail,parameter, success, fail)
            }
            //获取视频列表
            $scope.getVideoList = function (page, count, success, fail) {
                var parameter = {
                    "page": page,
                    "count": count,
                }
                console.log(parameter, success, fail)
                $scope.GET($scope.url.getVideoList, parameter, success, fail)
            }
            //获取视频详情
            $scope.getVideoDetail = function ( videoId, success, fail) {
                var parameter = {
                    "videoId":videoId,
                }
                console.log( parameter, success, fail)
                $scope.GET($scope.url.getVideoDetail,parameter, success, fail)
            }
            //获取话题列表
            $scope.getTopicList = function (page, count, success, fail) {
                var parameter = {
                    "page": page,
                    "count": count,
                    // "jsonp":"JSON_CALLBACK"
                }
                console.log(parameter, success, fail)
                $scope.GET($scope.url.getTopicList, parameter, success, fail)
            }
            //获取话题详情
            $scope.getTopicDetail = function ( topicId, success, fail) {
                var parameter = {
                    "topicId":topicId,
                }
                console.log( parameter, success, fail)
                $scope.GET($scope.url.getTopicDetail,parameter, success, fail)
            }
            //删除文章
            $scope.deleteArticle = function (articleId, success, fail) {
                var parameter = {
                    "articleId": articleId,
                }
                console.log("文章：" + parameter, success, fail)
                $scope.GET($scope.url.deleteArticle, parameter, success, fail)
            }
            //删除视频
            $scope.deleteVideo = function (videoId, success, fail) {
                var parameter = {
                    "videoId": videoId,
                }
                console.log("视频：" + parameter, success, fail)
                $scope.GET($scope.url.deleteVideo, parameter, success, fail)
            }
            //删除话题
            //添加文章
            $scope.addArticle =function (articleTitle, articleType, articleCover, articleContent, success, fail) {
                var parameter = {
                    "articleTitle": articleTitle,
                    "articleType": articleType,
                    "articleCover": articleCover,
                    "articleContent": articleContent,
                }
                console.log("添加文章的数据：" + parameter, success, fail)
                $scope.GET($scope.url.addArticle, parameter, success, fail)
            }
            //添加视频
            $scope.addVideo =function (videoTitle, videoUrl, videoCover, success, fail) {
                var parameter = {
                    "videoTitle": videoTitle,
                    "videoUrl": videoUrl,
                    "videoCover": videoCover,
                }
                console.log("添加视频的数据：" + parameter.articleTitle, parameter.articleTitle, articleType.articleCover,  articleType.articleContent,  success, fail)
                $scope.GET($scope.url.addVideo, parameter, success, fail)
            }
            //添加话题
            $scope.addTopic =function (articleTitle, articleCover, articleContent, success, fail) {
                var parameter = {
                    "articleTitle": articleTitle,
                    "articleType": "话题",
                    "articleCover": articleCover,
                    "articleContent": articleContent,
                }
                console.log("添加话题的数据：" + parameter, success, fail)
                $scope.GET($scope.url.addTopic, parameter, success, fail)
            }
            $scope.deleteTopic = function (topicId, success, fail) {
                var parameter = {
                    "topicId": topicId,
                }
                console.log("话题：" + parameter, success, fail)
                $scope.GET($scope.url.deleteTopic, parameter, success, fail)
            }
            //回复评论
            $scope.reply = function (commentContent, replyUserId, articleId, success, fail) {
              console.log(arguments)
               var parameter = {
                    "commentContent":commentContent,
                    "replyUserId":replyUserId,
                    "articleId":articleId,
                }
                console.log("回复评论：" + parameter, success, fail)
                $scope.GET($scope.url.reply, parameter, success, fail)
            }
            //图片上传
            $scope.imageUpload = function (formData, success, fail) {

                console.log("图片上传：" + formData, success, fail)
                $scope.formDataUpload($scope.url.imageUpload , formData, success, fail)
            }
            //视频上传
            $scope.videoUpload = function (video,  success, fail) {
                var parameter = {
                    "video":video,
                }
                console.log("视频上传：" + parameter, success, fail)
                $scope.GET($scope.url.videoUpload , parameter, success, fail)
            }

        }]);


// })